
#include "stm32f4xx_hal.h"
#include "drv_iic.h"
//#include "uart.h"
I2C_HandleTypeDef i2c;

int8_t iicinit(void)
{
    i2c.Instance            = I2C1;
    i2c.Init.ClockSpeed     = 100000;
    i2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    if(HAL_I2C_Init(&i2c) != HAL_OK)
	{
		
	}
	return 0;
}
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
{
	GPIO_InitTypeDef GPIO_InitType;         
	
	if(hi2c->Instance == I2C1){
		__HAL_RCC_I2C1_CLK_ENABLE();         
		__HAL_RCC_GPIOB_CLK_ENABLE();       
		//__HAL_RCC_AFIO_CLK_ENABLE();         
		//__HAL_AFIO_REMAP_I2C1_ENABLE();      
		
		GPIO_InitType.Pin = GPIO_PIN_6|GPIO_PIN_7;           
		GPIO_InitType.Mode = GPIO_MODE_AF_OD;        
		GPIO_InitType.Speed = GPIO_SPEED_FREQ_LOW; 
		GPIO_InitType.Alternate = GPIO_AF4_I2C1;
		HAL_GPIO_Init(GPIOB,&GPIO_InitType);     

//	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
//    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
//    GPIO_Init(GPIOB, &GPIO_InitStructure);

	}
}

int8_t iicdeinit(void)
{
    HAL_I2C_DeInit(&i2c);
    __HAL_RCC_I2C1_CLK_DISABLE();
		return 0;
}

int8_t write_reg(uint8_t addr, uint8_t *pdata, uint8_t size)
{
		
    
		return HAL_I2C_Master_Transmit(&i2c,addr,pdata,size,2000);
}

int8_t read_reg(uint8_t addr, uint8_t *pdata, uint8_t size)
{
		//HAL_MAX_DELAY
    //HAL_I2C_Master_Receive(&i2c,addr,pdata,size,2000);
		return HAL_I2C_Master_Receive(&i2c,addr,pdata,size,2000);
}
